import Vue from 'vue'
import VueRouter from 'vue-router'

/*
//配置懒加载
import Welcome from "../views/Welcome";
import Index from "../components/Index";
import Essay from "../components/normal/Essay";
import About from "../components/normal/About";
import Message from "../components/normal/Message";
import Project from "../components/normal/Project";
import Blog from "../components/normal/Blog";
import Error from "../views/Error";
import AdminIndex from "../components/admin/AdminIndex";
import Blogs from "../components/admin/Blogs";
import Blog_input from "../components/admin/Blog_input";
import Administrator from "../components/admin/Administrator";
import Types from "../components/admin/Types";
import Tags from "../components/admin/Tags";
import Comments from "../components/admin/Comments";
import Essays from "../components/admin/Essays";
import Projects from "../components/admin/Projects";
import Users from "../components/admin/Users";
import Home from "../views/Home";
*/


Vue.use(VueRouter)

//配置懒加载
//resolve => require(['@'],resolve)
const routes = [
  {
    path: '/error',
    component: resolve => require(['@/views/Error'],resolve)
  },
  {
    path:'/',
    name:'index',
    component: resolve => require(['@/views/Welcome'],resolve),   //在 Welcome 页面加载 NavBar 和 Footer 后，通过重定向转到 index，渲染 el-main 部分的 <route-view/>
    redirect:'/index',
    children:[
      {
        path:'index',
        component:resolve => require(['@/components/Index'],resolve)
      },
      {
        path:'essay', /* 不以 / 开头，就可以实现路径拼接了 */
        component:resolve => require(['@/components/normal/Essay'],resolve)
      },
      {
        path:'about',
        component:resolve => require(['@/components/normal/About'],resolve)
      },
      {
        path:'message',
        component:resolve => require(['@/components/normal/Message'],resolve)
      },
      {
        path:'project',
        component:resolve => require(['@/components/normal/Project'],resolve)
      },
      {
        path:'blogInfo',
        component:resolve => require(['@/components/normal/Blog'],resolve)
      }
    ]
  },
  {
    path: '/admin',
    component: resolve => require(['@/views/Home'],resolve),
    // 挂载路由导航守卫
    beforeEnter: (to, from, next) => {
      // to 将要访问的路径
      // from 代表从哪个路径跳转而来
      // next 是一个函数，表示放行
      // next() 放行  next('login') 强制跳转
      const userInfo = JSON.parse(window.sessionStorage.getItem('user'))
      // console.log(userInfo)
      if (!userInfo) return next('/error')
      else {
        const type = userInfo.type
        // console.log(type)
        /* 用户权限验证 */
        if (type !== '1') return next('/error')
        next()
      }
      next()
    },
    redirect: '/admin/index',
    children: [
      {path: '/admin/index', component: resolve => require(['@/components/admin/AdminIndex'],resolve)},
      {path: '/admin/blogs', component: resolve => require(['@/components/admin/Blogs'],resolve)},
      {path: '/admin/blog-input', component: resolve => require(['@/components/admin/Blog_input'],resolve)},
      {path: '/admin/administrator', component: resolve => require(['@/components/admin/Administrator'],resolve)},
      {path: '/admin/types', component: resolve => require(['@/components/admin/Types'],resolve)},
      {path: '/admin/tags', component: resolve => require(['@/components/admin/Tags'],resolve)},
      {path: '/admin/comments', component: resolve => require(['@/components/admin/Comments'],resolve)},
      {path: '/admin/essays', component: resolve => require(['@/components/admin/Essays'],resolve)},
      {path: '/admin/projects', component: resolve => require(['@/components/admin/Projects'],resolve)},
      {path: '/admin/users', component: resolve => require(['@/components/admin/Users'],resolve)}
    ]
  },
  {
    path: '**',//注意，是 **
    name: 404,
    component: resolve => require(['@/views/Error'],resolve)
  }
]

const router = new VueRouter({
  routes
})

export default router
